<template>
  <div>
    <div>
      <van-nav-bar 
      fixed=true 
      placeholder=true
      title="AddressList" left-text="" left-arrow @click-left="onClickFanHui">
      </van-nav-bar>
    </div>
    <div>
      <van-address-list v-model="chosenAddressId" :list="list" :disabled-list="disabledList" default-tag-text="默认"
        @add="onAdd" @edit="onEdit" />
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      chosenAddressId: "0",
      list: [
        {
          id: "",
          name: "",
          tel: "",
          address: "",
          isDefault: true,
        },
      ],
    };
  },

  created() {
    this.axios({
      method: "GET",
      url: "/aapi/addresslist",
    }).then((response) => {
      this.list = response.data;
      console.log(this.list);
      // this.list.forEach((element) => {
      //   list.id = element.id;
      //   console.log(list.id);
      //   list.name = element.name;
      //   list.tel = element.tel;
      //   list.address = element.address;
      //   list.isDefault = element.isDefault;
      // });
    });
  },

  methods: {
    onClickFanHui() {
      if (this.list != "" && this.chosenAddressId > 0) {

        this.list.forEach((element) => {
          if (element.id == this.chosenAddressId) {
            this.$store.commit("changeOderAdress", element.address)
            this.$store.commit("changeOrderTel", element.tel)
          }
        });

      }
      this.$router.go(-1)

    },
    onAdd() {
      this.$store.commit("getAddressId", 0);
      this.$store.commit("getAddressName", "");
      this.$store.commit("getAddressTel", "");
      this.$router.push("/addressedit");
    },
    onEdit(item, index) {
      this.$store.commit("getAddressId", item.id);
      this.$store.commit("getAddressName", item.name);
      this.$store.commit("getAddressTel", item.tel);
      this.$router.push({
        path: "/addressedit",
      });
    },
  },
};
</script>
